/**Invoice Class
 * @author Loc Phan
 * @version 1.0
 */

import java.util.LinkedList;
import java.util.ListIterator;


public class Invoice implements InvoiceADT
{
   private LinkedList<LineItem> text;
   private ListIterator<LineItem> current;
   private LineItem oldValue;
   private LineItem newValue;
   private int cursor = -1;
   
   /**
    * Constructs an invoice class
    */
   public Invoice()
   {
      text = new LinkedList<LineItem>();
      current = text.listIterator();
   }
   /* Returns true if successfully remove lines
    * (non-Javadoc)
    * @see InvoiceADT#remove(int, int)
    */
   @Override
   public boolean remove(int m, int n)
   {
      current = text.listIterator(m);
      n-=m-1;
      int counter = 0;
      boolean result = false;
      
      while(counter<n)
      {
         current.next();
         current.remove();
         result = true;
         counter++;
      }
      
      return result;
   }
   
   /*Returns true if added successfully
    * (non-Javadoc)
    * @see InvoiceADT#add(LineItem)
    */
   @Override
   public boolean add(LineItem s)
   {
      boolean result = false;
      
      
      if(cursor<0)
      {
         current.add(s);
         result = true;
      }
      else if(cursor<text.size())
      {
         current = text.listIterator(cursor);
         current.add(s);
         cursor++;
         result = true;
         
      }
      else
      {
         result = false;
      }
      return result;
      
   }
   /*Change cursor to position m
    * (non-Javadoc)
    * @see InvoiceADT#changeCursorTo(int)
    */
   @Override
   public void changeCursorTo(int m)
   {
      cursor = m;
      current = text.listIterator(m);
   }
   /*Replace old line with new line
    * (non-Javadoc)
    * @see InvoiceADT#replace(LineItem, LineItem)
    */
   @Override
   public boolean replace(LineItem anoldValue, LineItem anewValue)
   {
      this.oldValue = anewValue;
      this.newValue = anoldValue;
      boolean result = false;
      
      current = text.listIterator();
      while(current.hasNext())
      {
         LineItem nextline = current.next();
         if(nextline.getName().contains(anoldValue.getName()))
         {
            nextline = anewValue;
            current.set(nextline);
            result = true;
            break;
         }
      }
      return result;
      
   }
   /*Undo replace method
    * (non-Javadoc)
    * @see InvoiceADT#undo()
    */
   @Override
   public void undo()
   {
      this.replace(oldValue, newValue);
   }
   /*Returns a string representation
    * (non-Javadoc)
    * @see java.lang.Object#toString()
    */
   public String toString()
   {
      String result = "";
      int counter = 0;
      current = text.listIterator();
      while(current.hasNext())
      {
         if(counter == cursor)
         {
            result+=">"+" "+counter+" "+current.next().toString()+"\n";
         }
         else
         {
            result+="  "+counter+" "+current.next().toString()+"\n";
         }
         counter++;
      }
      return result;
   }

   
}
